<template>
	<el-dialog :visible.sync="addVisible" @close="$emit('update:addVisible',false)" width="50%" :show-close='false' class="addQcPlanDialog"
	 :closeOnClickModal='false'>
		<span slot="title" class="dialog-title">
		  	{{newOrEdit=='new'?'新增':(newOrEdit=='edit'?'编辑':'查看')}}文档类型
			<el-button  size="mini" @click="close" class="popupClose">关闭</el-button>
		</span>
		<el-form label-width="110px" :inline="false" class="demo-form-inline" :model="attachTypeForm" :rules="rules" ref="attachTypeForm">
			<el-form-item label="父节点" >
				<el-col :span="22">
					<el-input placeholder="文档类型" size="mini" v-model="attachTypeForm.parentName" disabled></el-input>
				</el-col>
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-col :span="22">
					<el-input placeholder="文档类型" size="mini" v-model="attachTypeForm.name"></el-input>
				</el-col>
			</el-form-item>
		</el-form>

		<span slot="footer" class="dialog-footer">
			<el-button type="primary" size="mini" @click="submitAttachType" :disabled="!isRepeat">提 交</el-button>
			<el-button  size="mini" @click="close">取 消</el-button>
		</span>
	</el-dialog>

</template>

<script>
	import { mapState } from 'vuex'

	export default {
		data() {
			return {
				attachTypeForm: {
					name: null,
					parent: null,
				},
				rules: {
					name: [
						{ type: 'string', required: true, message: '请填写名称', trigger: 'blur' }
					],
				},
				isRepeat: true,
			}
		},
		props: ['addVisible', 'newOrEdit', 'attachTypeInfo'],
		components: {

		},

		computed: {
			...mapState(['yesorno', 'enableStatus', 'submitType']),
		},
		mounted() {

		},
		watch: {
			addVisible(val, oldVal) {
				if (val) {
					console.log(this.attachTypeInfo)
					if (this.newOrEdit == 'new') {
						this.attachTypeForm.parent = this.attachTypeInfo.sign;
						this.attachTypeForm.parentName = this.attachTypeInfo.name;
					} else if (this.newOrEdit == 'edit') {
						for (var i in this.attachTypeInfo) {
							this.attachTypeForm[i] = this.attachTypeInfo[i]
						}
					}

				}
			}
		},
		methods: {
			submitAttachType() {
				this.$refs.attachTypeForm.validate((valid) => {  //表单验证
					if (valid) {
						var data = {
							name: this.attachTypeForm.name,
							sign: this.attachTypeForm.sign,
							parent : this.attachTypeForm.parent,
							parentName : this.attachTypeForm.parentName,
						}
						if (this.newOrEdit == 'new') {
							var url = "/technology/addLabFileType.htm"
							
						} else {
							var url = '/technology/updateLabFileType.htm';
							data.id = this.attachTypeForm.id;
						}
						this.isRepeat = false;
						this.$http({
							method: 'post',
							apiUrl: url,
							params: data,
							isJson: true,
						}).then(res => {
							this.isRepeat = true;
							if (res.code == 0) {
								this.$message({
									type: 'success',
									message: '提交成功!'
								});

								
								if (this.newOrEdit == 'edit') {
									// data.sign = this.attachTypeForm.sign
									this.$parent.handleNodeClick(data)
									this.$parent.readattachmentTypeList(this.attachTypeForm.id)
								} else {
									this.$parent.attachTypeInfo = {}
									this.$parent.readattachmentTypeList()
								}

								this.close('attachTypeForm');
							}
						}).catch(() => {

						});

					} else {
						return false;
					}
				});
			},
			//关闭
			close(formName) {
				this.$refs.attachTypeForm.resetFields();//重置表单
				this.isRepeat = true;
				// this.$emit()
				this.attachTypeForm = {
					name: null,
					parentName:null,

				};

				this.$emit('update:addVisible', false);
			},
		}
	}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
	.addQcPlanDialog .el-dialog {
		/*margin-top: 10vh !important;*/
		height: 50%;
	}

	.addQcPlanDialog .el-dialog .el-dialog__body {
		height: calc(100% - 85px);
		padding: 10px;
	}

	.addQcPlanDialog .heighHalf {
		height: calc(100% - 60px)
	}

	.addQcPlanDialog .heighHalf .height40 {
		height: 40px;
		line-height: 40px;
	}
</style>